﻿using eProcurement.BLL;
using eProcurement.DAL;
using eProcurement.helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement.members
{
    public partial class Company : System.Web.UI.Page
    {
        Manager manager = new Manager();

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                if ( Request[ "pid" ] != null )
                {
                    Project project = manager.GetProject( Convert.ToInt32( Request[ "pid" ] ) );
                    if ( project != null )
                    {
                        Hydrate( project );
                    }
                }
            }
        }

        private void Hydrate( Project project )
        {
            lblCompanyName.InnerText = project.Purchaser.CompanyName;
            lblAddress1.InnerText = project.Purchaser.Address1;
            lblAddress2.InnerText = project.Purchaser.Address2;
            lblCity.InnerText = project.Purchaser.City;
            lblWebsite.InnerHtml = "<a href='" + SiteFormatter.FormatURL( project.Purchaser.Website ) + "'>" + project.Purchaser.Website + "</a>";
            lblDescription.InnerText = project.Purchaser.Description;

            lblProjectName.InnerText = project.Title;
            lblProjectDescription.InnerText = project.Description;
            lblIndustryCategory.InnerText = project.IndustryCategory;
            lblClosureDate.InnerText = project.CloseDate.ToString( "ddd d MMM yyyy" );
            lblPostDate.InnerText = project.DatePosted.ToString( "ddd d MMM yyyy" );

            if ( project.State == ProjectState.OPEN.ToString() )
            {
                lnkUploadDocs.Attributes.Add( "href", "PlaceBid.aspx?pid=" + project.Id );
            }

            lnkDownloadScope.Attributes.Add( "href", "FileHandler.ashx?file=scope&pid=" + project.Id );
            lnkDownloadScope2.Attributes.Add( "href", "FileHandler.ashx?file=scope&pid=" + project.Id );
            lnkDownloadSupporting.Attributes.Add( "href", "FileHandler.ashx?file=supporting&pid=" + project.Id );
            lnkAnnouncements.Attributes.Add( "href", "Announcements.aspx?pid=" + project.Id );
            lnkViewBidders.Attributes.Add( "href", "Bidders.aspx?pid=" + project.Id );
            viewbidresult.Attributes.Add( "href", "Announcements.aspx?pid=" + project.Id );

            if ( !manager.IsPurchaser( Context.User.Identity.Name ) )
            {
                divBidders.Visible = false;
                divResults.Visible = false;
            }
        }
    }
}